#!/bin/ash
_is_online()                                                                       
{                                                                                      
        ping -q -c 1 -s 2 -W 3 114.114.114.114 > /dev/null                              
        return $?                                                                      
}                                                                                  
is_online()                                                                            
{                                                                                  
        local count=1                                                                  
        while test $count -lt 3; do                                                                             
                if _is_online; then                                                                 
                        return 0                                                   
                fi                                                                     
                count=`expr $count + 1`                                            
        done                                                                          
        return 1                                                                   
}
 
restart_4Gmodel()
{
	echo -e "AT+CFUN=1,1\r\n" >/dev/ttyUSB2
	sleep 20
}
#查询4G模块连接状态
#uci set network.wan.ifname='wwan0'
#uci set network.wan.proto='dhcp'
#ifup wan
simcard=`uci get uqmi.currsim.sim`
echo Start sim:$simcard
/root/switch_sim $simcard
sleep 15
#/etc/init.d/network restart
#wifi
false_cont=0
sim_scnt=0
wan_false_cnt=0
#######################################sleep 10
while true
do
	d=`uqmi -d  /dev/cdc-wdm0   --get-data-status`
	if [ "$d" == \"connected\" ];then
	  echo "was connected!"
	  false_cont=0
	  stat='ok'
	else
	  echo 'not connect'
	  wan_false_cnt=0
	  simcard=`uci get uqmi.currsim.sim`
	  if [ "$simcard" == "1" ];then
	  	echo "Use SIM1"
		apn=`uci get uqmi.card1.apn`
		auth=`uci get uqmi.card1.auth`
		username=`uci get uqmi.card1.username`
		password=`uci get uqmi.card1.password`
	  else
	    echo "Use SIM2"
		apn=`uci get uqmi.card2.apn`
		auth=`uci get uqmi.card2.auth`
		username=`uci get uqmi.card2.username`
		password=`uci get uqmi.card2.password`
	  fi

	  echo APN:$apn
	  echo AUTH:$auth
	  echo UserName:$username
	  echo PassWord:$password

	  uqmi -d /dev/cdc-wdm0 --start-network $apn --auth-type $auth --username $username --password $password --autoconnect >/dev/null
	  stat='false'
	  let "sim_scnt=$sim_scnt+1"
	  echo "false_cont:"$false_cont
	  echo "sim_scnt:"$sim_scnt
	    
		if [ $sim_scnt -ge 4 ];then
			sim_scnt=0
			let "false_cont=$false_cont+1"
			sim_mode=`uci get uqmi.sim_mode.mode`
			echo SIM switch mode:$sim_mode
			if [ "$sim_mode" == "auto" ];then
				if [ $simcard == "1" ];then
					uci set uqmi.currsim.sim=2
				else
					uci set uqmi.currsim.sim=1
				fi
				simcard=`uci get uqmi.currsim.sim`
			fi
			/root/switch_sim $simcard
			sleep 15
		fi

		if [ $false_cont -ge 30 ];then
		echo 'Restart 4G model!'
		restart_4Gmodel
		false_cont=0
	#	  	/etc/init.d/network restart
		fi
	  sleep 8
	  continue
	fi
	d=`cat /etc/config/MoBang | grep sim4g`
	if [ "$d" == "sim4g = on" ]; then
	    if ! $(is_online); then
	      if test $stat == ok; then
	      	uci set network.wan.ifname='wwan0'
		  	uci set network.wan.proto='dhcp'
			echo ifup wan
	      	ifup wan
			sleep 2
	      	let "wan_false_cnt=$wan_false_cnt+1"
	      	echo 'restart wan!'
	      	echo 'wan_false_cnt:'$wan_false_cnt
			if [ $wan_false_cnt -ge 3 ];then
			    echo 'Restart 4G model!'
			  	restart_4Gmodel
				/etc/init.d/network restart
			  	wan_false_cnt=0
				false_cont=0
			 fi
	      fi
	    else 
	    	echo 'online!'
	    	wan_false_cnt=0
			false_cont=0
			sleep 30
	    fi
	fi   
done
